package student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class StudentList {
    private List<Student> list;

    public StudentList() {
        list = new ArrayList<>();
    }

    public static void main(String[] args) {
        StudentList studentList = new StudentList();
        System.out.println(studentList.getList());
        System.out.println("---------");
        System.out.println(studentList.toString());

    }

    public int stringToInt(String s){
        int len = s.length();
        int sum = 0;
        int rg = 1;
        for (int i = 0; i < len; i++) {
            sum += (s.charAt(len-i-1)-'0')*rg;
            rg *= 10;
        }

        return sum;
    }

    public String toString() {
        list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        try {

            BufferedReader bf = new BufferedReader(new FileReader("src/main/java/student/student.txt"));
            bf.readLine();
            String txt = null;
            while ((txt = bf.readLine()) != null) {
                Student student = new Student();
                String[] s = txt.split("    ");
                student.setId(stringToInt(s[0]));
                student.setName(s[1]);
                student.setScore1(stringToInt(s[2]));
                student.setScore2(stringToInt(s[3]));
                student.setScore3(stringToInt(s[4]));
                sb.append(txt + "\n");
                list.add(student);
            }

        } catch (Exception e) {
            e.printStackTrace();

        }


        return sb.toString();
    }


    public List<Student> getList() {
        toString();
        return list;
    }

    public String searchForId(String text) {
        int id = stringToInt(text);
        StringBuilder stringBuilder = new StringBuilder();
        List<Student> zt  = list.stream().filter((stu) -> {
            return stu.getId() == id;
        }).collect(Collectors.toList());

        for (Student student : zt) {
            stringBuilder.append(student.getId()).append("  ").append(student.getName()).append("  ").append(student.getScore1()).append("  ").append(student.getScore2()).append("  ").append(student.getScore3()).append("  ");
        }

        return stringBuilder.toString();
    }

    public String listToString(List<Student> listz){
        StringBuilder sb = new StringBuilder();
        for (Student student : listz) {
            sb.append(student.getId()+"    ");
            sb.append(student.getName()+"    ");
            sb.append(student.getScore1()+"    ");
            sb.append(student.getScore2()+"    ");
            sb.append(student.getScore3()+"    ");
            sb.append("\n");
        }
        return sb.toString();
    }
}
